import { GetWorkList, DelWork, GetEditVersion } from "@/api";
import { useLike, useStar } from "~/hooks";
import { aesEncrypt } from "~/utils/encrypt";

const onVerify = ({ status, reason, callback }) => {
    switch (status) {
        case "0":
            Message().warning("审核中...");
            break;
        case "1":
            typeof callback === "function" && callback();
            break;
        case "2":
            Message().error("审核不通过！" + reason);
            break;
        default:
            break;
    }
};

export const useContent = () => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const dialogStore = useDialogStore();
    const tabActive = ref(1);
    const tabList = reactive([
        {
            label: "3D展厅",
            value: 1,
        },
        {
            label: "互动课件",
            value: 2,
        },
    ]);

    if (userStore.userInfo.level == 3) {
        tabList.push(
            {
                label: "消防作品",
                value: 4,
            },
            {
                label: "湛江作品",
                value: 5,
            },
            {
                label: "厦门电力作品",
                value: 6,
            },
            {
                label: "冶金作品",
                value: 7,
            },
            {
                label: "数据作品",
                value: 8,
            },
            {
                label: "医疗作品",
                value: 9,
            },
            {
                label: "水务作品",
                value: 10,
            },
            {
                label: "综合作品",
                value: 11,
            },
        );
    }
    const isLoading = ref(false);
    const list = ref([]);
    const keyword = ref("");
    const { onLike } = useLike();
    const { onStar } = useStar();

    const onEdit = async (item) => {
        if (pageStore.isMobile) {
            const res = await GetEditVersion();
            if (localStorage.getItem("editor_version") !== res.data) {
                localStorage.setItem("editor_version", res.data);
                dialogStore.isEditorUpdateTipsDialogVisible = true;
            } else {
                Message().warning("请在电脑端网站打开！网址www.ykjhr.com");
            }
        } else {
            let origin = GetTemplateLink3(tabActive.value);

            window.open(
                `https://${origin}/?${aesEncrypt(
                    "tid=" +
                    item.id +
                    "&type=editworks&uid=" +
                    userStore.userInfo.id
                )}`
            );
        }
    };

    const onShare = ({ url, name, status, reason }) => {
        let newUrl = GetWorkLink(url, tabActive.value);

        onVerify({
            status,
            reason,
            callback: () => {
                dialogStore.isShareTemplateDialogVisible = true;
                dialogStore.shareUrl = newUrl;
                dialogStore.shareName = name;
            },
        });
    };

    const onComment = async (item) => {
        onVerify({
            status: item.status,
            reason: item.reason,
            callback() {
                dialogStore.isCommentListDialogVisible = true;
                dialogStore.commentWorkId = item.id;
            },
        });
    };

    const onLikeAndVerify = async (item) => {
        onVerify({
            status: item.status,
            reason: item.reason,
            callback() {
                onLike(item);
            },
        });
    };

    const onStarAndVerify = async (item) => {
        onVerify({
            status: item.status,
            reason: item.reason,
            callback() {
                onStar(item);
            },
        });
    };

    const GetList = async (loading = true) => {
        loading && (isLoading.value = true);
        const res = await GetWorkList({
            name: keyword.value,
            type: tabActive.value,
        });
        loading && (isLoading.value = false);

        if (res.code == 200) {
            list.value = res.data;
        }
    };

    watch(tabActive, () => {
        GetList();
    });

    const onDelWork = async ({ id }) => {
        const res = await DelWork({
            id,
        });

        if (res.code == 200) {
            Message().success("删除成功！");
            GetList();
        } else {
            Message().error("删除失败！");
        }
    };

    const onJumpUrl = ({ url, status, reason }) => {
        const pageStore = usePageStore();
        let newUrl = GetWorkLink(url, tabActive.value);

        onVerify({
            status,
            reason,
            callback: () => {
                if (pageStore.isMobile) {
                    window.location.href = newUrl;
                } else {
                    window.open(newUrl);
                }
            },
        });
    };

    const onSearch = (value) => {
        keyword.value = value;
        GetList();
    };

    return {
        tabActive,
        tabList,
        isLoading,
        list,
        keyword,
        onJumpUrl,
        onShare,
        onEdit,
        onLikeAndVerify,
        onStarAndVerify,
        onDelWork,
        onSearch,
        onComment,
        GetList,
    };
};
